package com.xiaoyun.xpath;

import cn.wanghaomiao.xpath.model.JXDocument;
import com.xiaoyun.xpath.handler.field.FieldHandler;

import java.lang.reflect.Field;
import java.util.Map;

public class FieldTask<T> implements Runnable {


    private String typeName;
    private Class<T> clazz;
    private JXDocument jxDocument;
    private T t;
    private Map<String, String> mappingMap;
    private Field field;


    public FieldTask(String typeName, Class<T> clazz, JXDocument jxDocument, T t, Map<String, String> mappingMap, Field field) {
        this.typeName = typeName;
        this.clazz = clazz;
        this.jxDocument = jxDocument;
        this.t = t;
        this.mappingMap = mappingMap;
        this.field = field;
    }

    @Override
    public void run() {
        Class clazzTypeStrategy = TypeNameStrategy.getEnumType(typeName);
        if (clazz != null) {
            FieldHandler fieldHandler = null;
            try {
                fieldHandler = (FieldHandler) Class.forName(clazzTypeStrategy.getName()).newInstance();
                fieldHandler.handlerField(clazz, jxDocument, t, mappingMap, field);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
